package com.dddframework.kit.lang;

import cn.hutool.core.util.ArrayUtil;
import com.google.common.collect.Lists;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.List;

public final class ArrayKit {

    public static <T> T[] convert(Collection<T> collection) {
        if (collection == null || collection.size() == 0) {
            return null;
        }
        Class tClass = null;
        for (T t : collection) {
            tClass = t.getClass();
            break;
        }
        return collection.toArray((T[]) Array.newInstance(tClass, 0));
    }

    public static <T> List<T> convert(T[] array) {
        if (isEmpty(array)) {
            return null;
        }
        return Lists.newArrayList(array);
    }

    public static <T> T[] append(T[] buffer, T... newElements) {
        return ArrayUtil.append(buffer, newElements);
    }

    public static <T> boolean isNotEmpty(T[] array) {
        return null != array && array.length != 0;
    }

    public static <T> boolean isEmpty(T[] array) {
        return null == array || array.length == 0;
    }

}
